package com.ysd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.exception.RRException;
import com.ysd.modules.sys.dao.ChapterDao;
import com.ysd.modules.sys.dao.SysChapterVideoDao;
import com.ysd.modules.sys.dao.TopicSysDao;
import com.ysd.modules.sys.entity.Chapter;
import com.ysd.modules.sys.entity.SysChapterVideo;
import com.ysd.modules.sys.entity.Topic;
import com.ysd.modules.sys.model.StatusReq;
import com.ysd.modules.sys.service.ChapterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-07-13
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterDao, Chapter> implements ChapterService {
    @Resource
    private SysChapterVideoDao sysChapterVideoDao;
    @Resource
    private TopicSysDao topicSysDao;

    @Override
    public IPage<Chapter> listSearch(Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper();
        Integer page = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());

        Page<Chapter> currPage = new Page<>();
        currPage.setCurrent(page);
        currPage.setSize(limit);

        Object courseName = params.get("courseName");
        wrapper.like("course_name", courseName == null ? "" : courseName);

        Object name = params.get("name");
        wrapper.like("name", name == null ? "" : name);

        wrapper.ge("status", 0);

        IPage<Chapter> iPage = this.baseMapper.selectPage(currPage, wrapper);
        return iPage;
    }

    @Override
    public Chapter detailSearch(Integer id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 根据章节id或者视频名称查找视频
     *
     * @param cid
     * @param searchName
     * @return
     */
    @Override
    public IPage<Chapter> getVideoByChapter(Integer cid, String searchName, Integer page, Integer limit) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        Page<Chapter> currPage = new Page<>();
        currPage.setCurrent(page);
        currPage.setSize(limit);
        if (cid != null && cid != 0) {
            wrapper.eq("chapter_id", cid);
        }
        IPage<Chapter> iPage = this.baseMapper.selectPage(currPage, wrapper);
        for (int i = 0; i < iPage.getRecords().size(); i++) {
            Chapter chapter = iPage.getRecords().get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("chapterId", chapter.getId());
            if (searchName != null && !"".equals(searchName)) {
                map.put("searchName", searchName);
            }
            List<SysChapterVideo> sysChapterVideoList = baseMapper.getChapterVideo(map);
            chapter.setSysChapterVideoList(sysChapterVideoList);
        }
        return iPage;
    }

    /**
     * 增加视频
     *
     * @param sysChapterVideo
     * @return
     */
    @Override
    public int addSysChapterVideo(SysChapterVideo sysChapterVideo) {

        return sysChapterVideoDao.insert(sysChapterVideo);
    }

    /**
     * 删除视频
     *
     * @param id
     * @return
     */
    @Override
    public int delSysChapterVideo(Integer id) {
        return sysChapterVideoDao.deleteById(id);
    }

    /**
     * 修改视频
     *
     * @param sysChapterVideo
     * @return
     */
    @Override
    public int updateSysChapterVideo(SysChapterVideo sysChapterVideo) {
        return sysChapterVideoDao.updateById(sysChapterVideo);
    }

    /**
     * 根据id分页查询所有章节
     *
     * @param params
     * @return
     */
    @Override
    public IPage<Chapter> getChapterListByParams(Map<String, Object> params) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        Integer page = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());
        /**
         * 课程id
         */
        if (params.get("id") == null || params.get("id").equals("")) {
            throw new RRException("试卷没有选择课程,请选择课程");
        }
        Integer course_id = Integer.parseInt(params.get("id").toString());
        Page<Chapter> currPage = new Page<>();
        currPage.setCurrent(page);
        currPage.setSize(limit);
        wrapper.eq("status", 1);
        wrapper.eq("course_id", course_id);
        if (params.get("name") != null && !params.get("name").equals("")) {
            wrapper.like("name", params.get("name"));
        }
        IPage<Chapter> iPage = baseMapper.selectPage(currPage, wrapper);
        List<Chapter> chapterList = iPage.getRecords();
        for (int i = 0; i < chapterList.size(); i++) {
            Chapter chapter = chapterList.get(i);
            QueryWrapper<Topic> qr = new QueryWrapper<>();
            qr.eq("chapter_id", chapter.getId());
            Integer num = topicSysDao.selectCount(qr);
            chapter.setTnum(num);
        }
        return iPage;
    }

    @Override
    public void statusModify(StatusReq req) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id", req.getIds());
        updateWrapper.set("status", req.getStatus());
        this.update(updateWrapper);
    }
}

